package com.android.nextBus.Activities;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.android.nextBus.DB.SQLiteOperationsWrapper;
import com.android.nextBus.DB.StopItem;


public class ViewFavoritesActivity extends ListActivity {
	
	public static final String STOP_ID_KEY = "stop_id";
	public static final String STOP_NAME_KEY = "stop_name";
	private List<StopItem> stops;
	private int[] stopIDs;
	private String[] stopNames;
	private int[] stopLongs;
	private int[] stopLats;
	SQLiteOperationsWrapper db;
	public static Handler handler;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.favorites_layout);
		
//        handler = new Handler(){
//        	public void handleMEssage(Message msg){
//        		super.handleMessage(msg);
//        		
//        			switch(msg.what){
//        			
//        			case 0:
//        				finish();
//        				break;
//        		}
//        	}
//        };
        
        populateList();
		
		
    }
    
    public void populateList() {
    	
		db = new SQLiteOperationsWrapper(this);
		db.open();

		stops = db.getFavorites();
		
		db.close();
		
		int size;
		
		if(stops != null) {
			size = stops.size();
		} else {
			size = 0;
		}
		
		stopIDs = new int[size];
		stopNames = new String[size];
		stopLats = new int[size];
		stopLongs = new int[size];
		
		for(int i = 0; i < size; i++) {
			stopIDs[i] = stops.get(i).getStopID();	
			stopNames[i] = stops.get(i).getStopName() + ": " + stops.get(i).getStopDesc();
			stopLats[i] = (int) stops.get(i).getStopLat();
			stopLongs[i] = (int) stops.get(i).getStopLon();
		}
		
		ListAdapter mAdapter = new ArrayAdapter<String>(this, R.layout.route_row, stopNames);
		setListAdapter(mAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(), ViewTimesActivity.class);
				intent.putExtra(STOP_ID_KEY, stopIDs[position]);
				intent.putExtra(STOP_NAME_KEY, stopNames[position]);
				intent.putExtra(ViewStopsActivity.STOP_LAT_KEY, stopLats[position]);
				intent.putExtra(ViewStopsActivity.STOP_LONG_KEY, stopLongs[position]);
				startActivity(intent);
				
			}
		});
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	populateList();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_view_stops, menu);
        return true;
    }


}
